package sk.stancel.peter.sudoku.activities;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import sk.stancel.peter.sudoku.R;
import sk.stancel.peter.sudoku.SavedSudokuAdapter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class SavedSudokuActivity extends Activity implements OnItemClickListener{

	private List<String> list;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.saved_sudoku);
		
        list = new ArrayList<String>();
        
        File folder = new File(Environment.getExternalStorageDirectory(), "Sudoku");
        folder.mkdir();
        File files[] = folder.listFiles();
        if(files != null){
        	for (File file : files) {
    			if (!file.isDirectory()&& file.getName().endsWith(".sud") && file.canRead()) {
    				list.add(file.getAbsolutePath());
    			}
    		}
        }
        SavedSudokuAdapter adapter = new SavedSudokuAdapter(this, list);
        ListView listView = (ListView) findViewById(R.id.listview_saved_sudoku);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(this);
	}

	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Bundle bundle = new Bundle();
		bundle.putString("file_path", list.get(position));
		Intent intent = new Intent(SavedSudokuActivity.this, SudokuActivity.class);
		intent.putExtras(bundle);
		startActivity(intent);
	}
}
